//
//  JHRulerCell.m
//  iOS尺子控件
//
//  Created by JH on 2017/4/18.
//  Copyright © 2017年 JH. All rights reserved.
//

#import "JHRulerCell.h"
#import "JHRulerStyle.h"

@interface JHRulerCell()
@property (nonatomic, strong) CALayer *longLineLayer;
@property (nonatomic, strong) NSArray<CALayer *> *lineLayerArray;
@property (nonatomic, strong) UILabel *leadingLabel;
@property (nonatomic, strong) UILabel *trailingLabel;//尺子所有刻度最后一个label

@end
@implementation JHRulerCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

//- (void)awakeFromNib{
//    [super awakeFromNib];
//    [self setupUI];
//}
- (void)setupUI{
    self.longLineLayer = [CALayer layer];

    self.longLineLayer.frame = CGRectZero;

    //水平长线
    self.longLineLayer.backgroundColor = self.rulerStyle.lineColor.CGColor;
    [self.layer addSublayer:self.longLineLayer];
    
    //刻度线的layer
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < 11; i++) {
        CALayer *subLayer = [CALayer layer];
        subLayer.frame = CGRectZero;
        [array addObject:subLayer];
        [self.layer addSublayer:subLayer];
    }
    self.lineLayerArray = array;

#warning 设置旋转90度的字体
    self.leadingLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:self.leadingLabel];
    self.trailingLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:self.trailingLabel];
    [self updateUIWithRulerStyle:self.rulerStyle atIndexPath:nil];

}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGRect leadingRect = self.leadingLabel.frame;

    CGRect trailingRect = self.trailingLabel.frame;
    
    if (self.rulerStyle.showType == rulerViewshowHorizontalType) {
        leadingRect.origin.x = - (leadingRect.size.width / 2);//第一个长刻度线左移半自己的一半
        leadingRect.origin.y = self.rulerStyle.longLineLength;//第一个长刻度的高度
        trailingRect.origin.x = CGRectGetWidth(self.bounds) - (trailingRect.size.width / 2);
        trailingRect.origin.y = self.rulerStyle.longLineLength;

    }else{
        leadingRect.origin.x = self.rulerStyle.longLineLength * 1.1;
        leadingRect.origin.y = - (leadingRect.size.height / 2);
        trailingRect.origin.x = self.rulerStyle.longLineLength;
        trailingRect.origin.y = CGRectGetHeight(self.bounds) - (trailingRect.size.height / 2);
    }
    self.leadingLabel.frame = leadingRect;
    self.trailingLabel.frame = trailingRect;

}
#pragma mark - PublicMethod
- (void)updateUIWithRulerStyle:(JHRulerStyle *)rulerStyle atIndexPath:(NSIndexPath *)indexPath{
    self.rulerStyle = rulerStyle;
    
    CGFloat startPoint = 0;//每条刻度线开始点
    CGFloat endPoint = 0;//每条刻度线结束点
    CGFloat spacing = self.rulerStyle.spacing;
    CGFloat offsetValue = self.rulerStyle.minValue + indexPath.row * self.rulerStyle.accuracy * 10;//是相对于尺子的精度的偏移量与实际尺寸无关
    for (int i = 0; i < 11; i++) {
        CALayer *subLayer = self.lineLayerArray[i];
        startPoint = i * spacing;
        if (i == 0) {
            endPoint = self.rulerStyle.longLineLength;
            self.leadingLabel.font = self.rulerStyle.font;
            self.leadingLabel.textColor = self.rulerStyle.lineColor;
            self.leadingLabel.text = [NSString stringWithFormat:@"%.0f",offsetValue + i * self.rulerStyle.accuracy];
            [self.leadingLabel sizeToFit];
        }else if (i == 5){
            endPoint = (self.rulerStyle.longLineLength + self.rulerStyle.shortLineLength) / 2.0;
        }else if (i == 10){
            endPoint = self.rulerStyle.longLineLength;
            self.trailingLabel.font = self.rulerStyle.font;
            self.trailingLabel.textColor = self.rulerStyle.lineColor;
            self.trailingLabel.text = [NSString stringWithFormat:@"%.0f",offsetValue + i * self.rulerStyle.accuracy];
            [self.trailingLabel sizeToFit];
        }else{
            endPoint = self.rulerStyle.shortLineLength;
        }
        
        if (self.rulerStyle.showType == rulerViewshowHorizontalType) {
            self.longLineLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 1);
            subLayer.frame = CGRectMake(startPoint, 0, 1, endPoint);//刻度线frame

        }else{
            self.longLineLayer.frame = CGRectMake(0, 0, 1, CGRectGetHeight(self.bounds));
            subLayer.frame = CGRectMake(0, startPoint, endPoint, 1);//刻度线frame

        }
        self.longLineLayer.backgroundColor = self.rulerStyle.lineColor.CGColor;

        subLayer.backgroundColor = self.rulerStyle.lineColor.CGColor;
    }
    
    
    
    //判断trailingLabel与leadingLabel重合隐藏
    //除了最后一个其他都隐藏
    if ((self.rulerStyle.maxValue - self.rulerStyle.minValue) / self.rulerStyle.accuracy / 10 - 1 == indexPath.row) {
        self.trailingLabel.hidden = NO;
    }else{
        self.trailingLabel.hidden = YES;
    }

}

#pragma mark - 懒加载
-(JHRulerStyle *)rulerStyle{
    if (!_rulerStyle) {
        _rulerStyle = [[JHRulerStyle alloc] init];
    }
    return _rulerStyle;
}
@end



@implementation UICollectionReusableView (JHIdentifier)

+ (NSString *)jh_reuseIdentifier {
    return NSStringFromClass(self);
}


@end







